﻿using CIS.DB;
using CIS.Web.Common;
using CIS.Web.Models;
using CIS.Web.Properties;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[RoutePrefix("ConferenceMail")]
	public class ConferenceMailController : BaseDbController
	{
		[Route("Index/{ConferenceId}")]
		public async Task<ActionResult> Index(string conferenceId)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", conferenceId);

			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.Status == ConstValues.Status.正常 &&
						x.ID == conferenceId);

			ViewBag.ConferenceId = conferenceId;
			ViewBag.ConferenceName = conferenceMaster.ConferenceName;
			ViewBag.ControllerName = Resources.ConferenceMail;
			ViewBag.Language = conferenceMaster.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn;

			var model = new ConferenceMailModel();
			model.ConferenceId = conferenceId;
			LoginUser.ConferenceFunctionIdList = Utility.GetConferenceFunctionIdList(conferenceId);
			return View(model);
		}

		public async Task<JsonCamelCaseResult> GetData(int? page, int? rows, string sort, string order, ConferenceMailModel model)
		{
			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == model.ConferenceId);

			#region 检索邮箱
			var conferenceMail = DbContext.ConferenceMailServerMaster.AsNoTracking()
					.FirstOrDefault(x => x.Status == ConstValues.Status.正常 && x.ConferenceId == model.ConferenceId && x.MailType == ConstValues.MailType.会议邮箱);
			var attendenceMail = DbContext.ConferenceMailServerMaster.AsNoTracking()
					.FirstOrDefault(x => x.Status == ConstValues.Status.正常 && x.ConferenceId == model.ConferenceId && x.MailType == ConstValues.MailType.注册服务邮箱);
			var contributeMail = DbContext.ConferenceMailServerMaster.AsNoTracking()
					.FirstOrDefault(x => x.Status == ConstValues.Status.正常 && x.ConferenceId == model.ConferenceId && x.MailType == ConstValues.MailType.投稿服务邮箱);
			var bookingMail = DbContext.ConferenceMailServerMaster.AsNoTracking()
					.FirstOrDefault(x => x.Status == ConstValues.Status.正常 && x.ConferenceId == model.ConferenceId && x.MailType == ConstValues.MailType.酒店交通服务邮箱);

			var result = new List<ConferenceMailModel>();
			result.Add(new ConferenceMailModel
			{
				ID = ConstValues.MailID.会议注册,
				Remark = ConstValues.MailSubject.会议注册,
				Subject = conferenceMaster.IsInternational ? ConstValues.MailSubjectEn.会议注册 : ConstValues.MailSubject.会议注册,
				MailType = attendenceMail != null ? Resources.MailAttendence: conferenceMail != null ? Resources.MailConference: string.Empty,
				Mail = attendenceMail != null ? attendenceMail.Mail : conferenceMail != null ? conferenceMail.Mail : string.Empty,
			});
			result.Add(new ConferenceMailModel
			{
				ID = ConstValues.MailID.缴费成功,
				Remark = ConstValues.MailSubject.缴费成功,
				Subject = conferenceMaster.IsInternational ? ConstValues.MailSubjectEn.缴费成功 : ConstValues.MailSubject.缴费成功,
				MailType = attendenceMail != null ? Resources.MailAttendence : conferenceMail != null ? Resources.MailConference : string.Empty,
				Mail = attendenceMail != null ? attendenceMail.Mail : conferenceMail != null ? conferenceMail.Mail : string.Empty,
			});
			result.Add(new ConferenceMailModel
			{
				ID = ConstValues.MailID.会议投稿,
				Remark = ConstValues.MailSubject.会议投稿,
				Subject = conferenceMaster.IsInternational ? ConstValues.MailSubjectEn.会议投稿 : ConstValues.MailSubject.会议投稿,
				MailType = contributeMail != null ? Resources.MailContribute : conferenceMail != null ? Resources.MailConference : string.Empty,
				Mail = contributeMail != null ? contributeMail.Mail : conferenceMail != null ? conferenceMail.Mail : string.Empty,
			});
			result.Add(new ConferenceMailModel
			{
				ID = ConstValues.MailID.审稿结果,
				Remark = ConstValues.MailSubject.审稿结果,
				Subject = conferenceMaster.IsInternational ? ConstValues.MailSubjectEn.审稿结果 : ConstValues.MailSubject.审稿结果,
				MailType = contributeMail != null ? Resources.MailContribute : conferenceMail != null ? Resources.MailConference : string.Empty,
				Mail = contributeMail != null ? contributeMail.Mail : conferenceMail != null ? conferenceMail.Mail : string.Empty,
			});
			result.Add(new ConferenceMailModel
			{
				ID = ConstValues.MailID.酒店预订,
				Remark = ConstValues.MailSubject.酒店预订,
				Subject = conferenceMaster.IsInternational ? ConstValues.MailSubjectEn.酒店预订 : ConstValues.MailSubject.酒店预订,
				MailType = bookingMail != null ? Resources.MailHotelTraffic : conferenceMail != null ? Resources.MailConference : string.Empty,
				Mail = bookingMail != null ? bookingMail.Mail : conferenceMail != null ? conferenceMail.Mail : string.Empty,
			});
			result.Add(new ConferenceMailModel
			{
				ID = ConstValues.MailID.交通预订,
				Remark = ConstValues.MailSubject.交通预订,
				Subject = conferenceMaster.IsInternational ? ConstValues.MailSubjectEn.交通预订 : ConstValues.MailSubject.交通预订,
				MailType = bookingMail != null ? Resources.MailHotelTraffic : conferenceMail != null ? Resources.MailConference : string.Empty,
				Mail = bookingMail != null ? bookingMail.Mail : conferenceMail != null ? conferenceMail.Mail : string.Empty,
			});
			#endregion

			int total = result.Count;

			if (!string.IsNullOrEmpty(sort))
			{
				result = result.OrderBy(sort + ConstValues.chrSpace + order).ToList();
			}

			return JsonCamelCase(new
			{
				Total = total,
				Rows = result,
			},
			JsonRequestBehavior.AllowGet);
		}

		public async Task<JsonCamelCaseResult> GetEditData(ConferenceMailModel model)
		{
			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == model.ConferenceId);
			var conferenceMail = await DbContext.ConferenceMailMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.MailId == model.MailId && x.ConferenceId == model.ConferenceId);
			var systemMail = await DbContext.SysMailMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == model.MailId);

			MailMessage message = Utility.GetMailContent(new MailModel {
				IsPreview = true,
				MailRemark = systemMail.Remark,
				Language = conferenceMaster.IsInternational ? ConstValues.Language.En : ConstValues.Language.Cn,
				ConferenceId = model.ConferenceId
			});

			var result = new
			{
				ID = systemMail.ID,
				Content = message.Body.Replace("\"", "'"),
				CustomDefinition = conferenceMail != null ? conferenceMail.CustomDefinition : string.Empty
			};

			return JsonCamelCase(Utility.CreateSuccessJsonResult(string.Empty, result),
					JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		[ValidateInput(false)]
		[Route("Modify/{ConferenceId}")]
		public async Task<JsonResult> Modify(ConferenceMailModel model)
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId);

			var result = await DbContext.ConferenceMailMaster
					.FirstOrDefaultAsync(x => x.MailId == model.MailId && x.ConferenceId == model.ConferenceId);

			if (result != null)
			{
				result.CustomDefinition = model.CustomDefinition;
				result.Updater = LoginUser.UserName;
				result.UpdateDate = DateTime.Now;
				result.UpdateIP = LoginUser.IP;
			}
			else
			{
				var systemMail = await DbContext.SysMailMaster.AsNoTracking()
						.FirstOrDefaultAsync(x => x.ID == model.MailId);
				var conferenceMail = new ConferenceMailMaster
				{
					ID = Utility.GetTableId(ConstValues.TableName.ConferenceMailMaster),
					ConferenceId = model.ConferenceId,
					MailId = systemMail.ID,
					CustomDefinition = model.CustomDefinition,
					Status = ConstValues.Status.正常,
					Sort = ConstValues.DefaultSort,
					Remark = systemMail.Remark,
					Creator = LoginUser.UserName,
					CreateIP = LoginUser.IP,
					CreateDate = DateTime.Now
				};

				DbContext.ConferenceMailMaster.Add(conferenceMail);
			}
			await DbContext.SaveChangesAsync();

			return JsonCamelCase(Utility.CreateSuccessJsonResult(Resources.SuccessSubmit),
					JsonRequestBehavior.AllowGet);
		}
	}
}